<?php

namespace App\Api\Controllers;

use App\Models\SmsCode;
use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
use iscms\Alisms\SendsmsPusher as Sms;


/**
 * Class SmsCodeController
 * @package App\Api\Controllers
 * 短信验证码
 */
class SmsCodeSendController extends Controller
{
    use Helpers;
    const SmsExpire=1500;//验证码有效期
    private $sms;
    public function __construct(Sms $sms)
    {
        $this->sms=$sms;
    }
    /**
     *阿里大于短信插件方法,参数传入一个11位的数字
     *$phone 用户的手机号
     *$num,需要发送的验证码
     *$name 短信签名,阿里大鱼申请的短信签名的名字
     *$content 替换短信模板中的变量,JSON字符串格式,所有值都是字符串,不能有整形
     *$code 阿里大鱼申请的短信模板编号
     */
    public function alidayu($phone,$num)
    {
        //要发送的号码
        if(strlen($phone)!=11){
            return ['result'=>0,'message'=>'手机号错误'];
        }
        //随机验证码
        $smsParams = [
            //阿里大于定义的变量名
            'code'  => "$num",
            'product' => '吾校吾书'
        ];
        //阿里大于短信签名
        $name = '领呗商城';
        //数组,要发送的值
        $content = json_encode($smsParams);
        //你的短信模板ID
        $code = 'SMS_9545208';
        $data=$this->sms->send($phone,$name,$content,$code);
        //判断是否发送成功
        if(property_exists($data,'result')){
            //把手机号码存入redis缓存 设置600秒的有效时间
            //发送成功返回成功
            return ['result'=>1,'message'=>'已发送'];
        }else{
            //发送失败返回失败
            return ['result'=>0,'message'=>'发送失败'];
        }
    }



    /**
     * 发送短信验证码
     * @param Request $request
     * @return mixed
     */
    public function smsCode(Request $request){
        $user=$this->auth->user();
        $phoneNumber=$request->input('phone_number');
        if($phoneNumber && strlen($phoneNumber)==11){
            //检查当前获取验证的的号码是否和自己当前绑定的号码一致
            if($phoneNumber==$user->phone_number){
                return $this->response()->array([
                    "message" => "手机号码正在使用",
                    "status_code" => 201
                ]);
            }
            //先在数据库查询是否存在90秒内，该电话号码未被使用的短信验证码，
            $code=SmsCode::where([['phone_number',$phoneNumber],['ctime','>',time()-self::SmsExpire],['status',0]])->first();
            if($code){
                $smsCode=$code->code;
                $code->ctime=time();
                $code->save();
            }else{
                //生成一个验证码
                $smsCode=random_int(1000,9999);
                SmsCode::create([
                    'phone_number'=>$phoneNumber,
                    'ctime'=>time(),
                    'code'=>$smsCode,
                    'status'=>0
                ]);
            }
            //发送短信
            $res=$this->alidayu($phoneNumber,$smsCode);
            if($res['result']){
                return $this->response()->array([
                    "message" => $res['message'],
                    'sms_code'=>$smsCode,
                    "status_code" => 200
                ]);
            }else{
                return $this->response()->array([
                    "message" => $res['message'],
                    "status_code" => 201
                ]);
            }

        }else{
            return $this->response()->array([
                "message" => "请求不合法",
                "status_code" => 403
            ]);
        }
    }



}